#!/usr/bin/env python 
import threading
from socket import * 
from time import sleep,ctime

#HOST = '192.168.1.9'
HOST = '127.0.0.1'
PORT = 8888
BUFSIZ = 1024 
ADDR = (HOST, PORT) 
IS_RUN = 1

def dosome():
	print 
	print ctime(),">>","I am socket."
	print "from socket"
	print "hello..."
	print ">"
	print ">"


def asocket():
	while True: 
		if IS_RUN == 0:
			break
		#dosome()
		asock_data = tcpCliSock.recv(BUFSIZ) 
		if not asock_data: 
		    break 
		print asock_data.strip()
		#sleep(5)

if __name__ == "__main__":
	print "Connect to tcpserver..."
	tcpCliSock = socket(AF_INET, SOCK_STREAM) 
	tcpCliSock.connect(ADDR) 

	print "Start receive thread..."
	t_asocket = threading.Thread(target=asocket,args=())
	t_asocket.setDaemon(True)
	t_asocket.start()

	while True: 
		indata = raw_input('') 
		if (indata=="EXIT"):
			IS_RUN = 0
			tcpCliSock.close()
			print "exited."
			exit()
		print "Input:",indata
		tcpCliSock.send('%s' % indata) 
 


